package com.threebrothers.lib_mvi.base

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.PersistableBundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.jaeger.library.StatusBarUtil
import kotlin.math.ceil

abstract class BaseActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        injectARouter()
        super.onCreate(savedInstanceState)

        //设置布局
        if (-1 == getLayoutId()){
            setContentView(getLayoutRootView())
        }else{
            setContentView(getLayoutId())
        }
        //设置状态栏
        StatusBarUtil.setColorNoTranslucent(this, getStatusBarColor())
        //初始化环境
        initEnv()
        //初始化View
        initView()
        //初始化数据
        initData()
        //初始化事件监听
        initEvent()
    }

    protected open fun initEnv() {

    }
    protected open fun initView() {

    }
    protected open fun initData() {

    }
    protected open fun initEvent() {

    }

    protected open fun getStatusBarColor(): Int {
        return Color.TRANSPARENT
    }

    abstract fun getLayoutRootView(): View

    /**
     * 获得布局id
     */
    protected open fun getLayoutId(layout : Int = -1) : Int{
        return layout
    }




    protected fun injectARouter() {


    }

    /**
     * 显示提示消息
     */
    private val handle : Handler = Handler(Looper.getMainLooper())
    fun showMsg(msg:String){
        if (Looper.getMainLooper().thread == Thread.currentThread()){
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
        }else{
            handle.post{
                Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
            }
        }
    }


    /**
     * 组件内跳转到指定页面
     */
    fun routerActivity(target : Class<*>){
        startActivity(Intent(this,target))
    }
    /**
     * 组件内跳转到指定页面并传递参数
     */

    fun routeActivity(target:Class<*>,params:Bundle){
        val intent = Intent(this,target)
        intent.putExtra("params",params)
        startActivity(intent)
    }

    fun addFragment(fragment:Fragment,tag : String){
        supportFragmentManager.beginTransaction().add(fragment,tag).commitAllowingStateLoss()

    }

    fun hideFragment(tag : String){
        supportFragmentManager.findFragmentByTag(tag)
            ?.let {
                supportFragmentManager.beginTransaction().hide(it).commitAllowingStateLoss()
            }
    }
    fun showFragment(tag:String){
        supportFragmentManager.findFragmentByTag(tag)?.let {
            supportFragmentManager.beginTransaction().show(it).commitAllowingStateLoss()

        }
    }
    fun replaceFragment(containerId:Int,fragment:Fragment){
        supportFragmentManager.beginTransaction().replace(containerId,fragment).commitAllowingStateLoss()

    }
    fun switchFragment(fl:Int,from:Fragment?,to:Fragment){
        if (!to.isAdded){
            supportFragmentManager.beginTransaction().hide(from!!).add(fl,to).commitAllowingStateLoss()
        }else{
            supportFragmentManager.beginTransaction().hide(from!!).show(to).commitAllowingStateLoss()
        }
    }
    fun closeThis(){
        finish()
    }
    /**
     * 获取状态栏高度
     */
    private fun getStatusBarHeight(): Int {
        val identifier = resources.getIdentifier("status_bar_height", "dimen", "android")
        return if (identifier > 0) {
            resources.getDimensionPixelSize(identifier)
        } else {
            val density = resources.displayMetrics.density
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                ceil(24 * density).toInt()
            } else {
                ceil(25 * density).toInt()
            }
        }
    }
}